﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Web/UserController.cs
// Author: Sohobloo
// CreateTime: 2013/07/01 10:37
// UpdateTime: 2013/07/02 16:45

#endregion

#region 引用

using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web.Mvc;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoAuth.Service;
using Sohobloo.SoAuth.Web.Models;
using Sohobloo.SoMcCoffee.Public.Security;

#endregion

namespace Sohobloo.SoAuth.Web.Controllers
{
    [Export]
    public class UserController : Controller
    {
        [Import]
        private IUserService UserService { get; set; }

        public ActionResult Index()
        {
            var userList = UserService.GetUserList();

            var userModelList = userList.Select(
                u => new UserModel
                {
                    Id = u.Id,
                    UserName = u.UserName,
                    NickName = u.NickName,
                    Email = u.Email,
                    UserStatus = u.UserStatus
                });

            return View(userModelList);
        }

        public ActionResult Add()
        {
            var userModel = new UserModel();

            return View(userModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(UserModel userModel)
        {
            var user = new SoAuthUser
            {
                UserName = userModel.UserName.Trim(),
                PasswordHash = Md5.GetSaltedMd5(userModel.Id + userModel.Password.Trim()),
                NickName = userModel.NickName.Trim(),
                Email = userModel.Email.Trim(),
                UserStatus = userModel.UserStatus.Value
            };

            UserService.CreateUser(user);

            return RedirectToAction("Index");
        }

        public ActionResult Edit(Guid id)
        {
            var user = UserService.GetUser(id);

            var userModel = new UserModel
            {
                Id = user.Id,
                UserName = user.UserName,
                NickName = user.NickName,
                Email = user.Email,
                UserStatus = user.UserStatus
            };

            return View(userModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(UserModel userModel)
        {
            var user = UserService.GetUser(userModel.Id);
            user.UserName = userModel.UserName.Trim();
            if(userModel.Password != null)
            {
                user.PasswordHash = Md5.GetSaltedMd5(userModel.Id + userModel.Password.Trim());
            }
            user.NickName = userModel.NickName.Trim();
            user.Email = userModel.Email.Trim();
            user.UserStatus = userModel.UserStatus.Value;

            UserService.UpdateUser(user);

            return RedirectToAction("Index");
        }

        public ActionResult Delete(Guid id)
        {
            UserService.DeleteUser(id);

            return RedirectToAction("Index");
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Search(string keyword)
        {
            var nameList = UserService.QueryUserNameByKeyword(keyword);
            return Json(nameList, JsonRequestBehavior.AllowGet);
        }
    }
}